import QtQuick 1.0
import com.nokia.symbian 1.1
import "../delegates"
import "../js/core.js" as Core

Page {
    id : root

    property string topicName
    property int topicID

    TitleHeader {
        id: header
        text:   "Notes - " + topicName
    }

    ListModel {
        id: noteModel
    }

    ListView {
        id:  noteView
        anchors {
            left: root.left; right: root.right;
            top: header.bottom; bottom: parent.bottom;
        }
        model: noteModel
        delegate: ItemDelegate {
            id: itemDelegate
            name: model.noteName
            checked: model.checked
            onClicked: {
                root.pageStack.push(window.noteDetails, {noteName: model.noteName, noteBody: model.noteBody, noteID: model.noteID, state: "edit"});
            }
            onCheckBoxClicked: {
                if(checked != model.checked) {
                    noteModel.setProperty(index, 'checked', checked);
                    Core.updateNoteChecked(model);
                }
            }
        }
        clip: true
    }

    TextInputDialog{
        id: noteCreateDialog
        dialogTitleText: "Create Note"
        placeHolderText:"Enter Note Name.."

        onAccepted: {
            if(userInput !== "") {
                Core.addNote(topicID, userInput);
                updateUi();
            }
        }
    }

    ConfirmDialog{
        id:deleteDialog
        dialogTitleText: "Delete Confirmation"
        messageText: "      Are you sure you want\n to delete the selected notes?"

        onAccepted: {
            Core.deleteNotes();
            updateUi();
        }
    }

    tools: ToolBarLayout {
        ToolButton{
            iconSource: "toolbar-back"
            onClicked:
                root.pageStack.pop();
        }
        ToolButton{
            iconSource: "toolbar-add"
            onClicked:{
                root.pageStack.push(window.noteDetails, {topicID: topicID, state: "add"});
            }
        }
        ToolButton{
            iconSource: "toolbar-delete"
            onClicked:
            {
                deleteDialog.open();
            }
        }
    }

    onStatusChanged: {
        if(status == PageStatus.Activating) {
            updateUi();
        }
    }

    function updateUi()
    {
        noteView.model = 0;
        Core.readNotes(noteModel, topicID);
        noteView.model = noteModel;
    }

    Component.onCompleted: {
        Core.openDB();
    }
}
